<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>统计图表-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
        <meta name="renderer" content="webkit|ie-comp|ie-stand">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta http-equiv="Cache-Control" content="no-siteapp" />
		<link rel="stylesheet" href="../../static/css/font.css">
		<link rel="stylesheet" href="../../static/css/weadmin.css">
    </head>
    <body>
        <div class="weadmin-body">
            <blockquote class="layui-elem-quote">
                特别声明：本网站的初衷是希望自已及身边的朋友看真假降价，不被商家的各类促销活动给忽悠通过查询商品历史价格，能更清晰了解商家的活动是否为虚假促销以及促销的力度。
            </blockquote>
            <div class="layui-row ">
                <form class="layui-form  layui-col-md12 layui-col-md-offset4 we-search">
                    商品搜索：
                    <div class="layui-inline">
                        <input type="text" name="username" placeholder="请输入商品url" autocomplete="off"  id="searchVal" class="layui-input" />
                    </div>
                    <button class="layui-btn" type="button" id="search">
                        <i class="layui-icon layui-icon-search" ></i>
                    </button>
                    <button class="layui-btn  layui-btn-warm" type="button" id="subscribe">商品订阅</button>
                    <button class="layui-btn  layui-btn-warm" type="button" id="predict">商品价格预测</button>
                    <button class="layui-btn  layui-btn-warm" type="button" id="board">商品热搜榜</button>
                </form>
                <input type="text" style="visibility: hidden;" id="hiddenCID" />
                <input type="text" style="visibility: hidden;" id="hiddenPrice" />
            </div>
            <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
            <div id="main" style="width: 100%;height:400px;"></div>
            <blockquote class="layui-elem-quote">
                注意：本案例的Echarts图表库由cdn引入，需要在线才能正常使用，若要离线使用，请至Echarts官网下载。
            </blockquote>

        </div>
        <script src="//cdn.bootcss.com/echarts/4.0.2/echarts.min.js"></script>
        <script src="/static/js/jquery-latest.min.js"></script>
        <script src="../../lib/layui245/layui.js" charset="utf-8"></script>
       <!-- <script src="\lib\layui230\lay\modules\layer.js"></script>-->
        <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '商品历史价格查询'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:[]
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['周一','周二','周三','周四','周五','周六','周日']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                 //   name:'邮件营销',
                    type:'line',
                    stack: '总量',
                    data:[120, 132, 101, 134, 90, 230, 210]
                }
            ]
        };
        //调用setInterval动态渲染参数
        function getPrice(url,num)
        {

            $.ajax({
                url: '/commodity/findHistory', //地址
                data:{url:url},
                dataType: 'json', //数据类型
                type: 'POST', //类型
                timeout: 2000, //超时
                //请求成功
                success: function(data, status) {
                    //默认取五个
                    $("#hiddenCID").val(data.list[0].cId)
                    Xarray=new Array(data.total-1);
                    Yarray=new Array(data.total-1);
                    for(i=0;i<Xarray.length;i++){
                        Xarray[i]=data.list[i+1].cDate.substring(0,10);
                        Yarray[i]=data.list[i+1].cPrice;
                    }
                    myChart.setOption({
                        xAxis: {
                            data: Xarray
                        },
                        legend: {
                            data:[data.list[0].cTitle+'']
                        },
                        series: [{
                            name: [data.list[0].cTitle+''],
                            data: Yarray
                        }]
                    });
                },
                //失败/超时
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        log.success('請求超時');
                        setTimeout(function() {
                            log.success('重新请求');
                        }, 2000);
                    }
                }
            });

        }
        function subscribeCommodity(commodityId){
            $.ajax({
                url: '/commodity/subscribe', //地址
                data:{
                  "c_id": commodityId,
                    "username": "1098294815@qq.com",
                    "sub_price":  20
                },
                dataType: 'json', //数据类型
                type: 'GET', //类型
                timeout: 2000, //超时
                //请求成功
                success: function(data, status) {
                    console.log(data)
                    if(data==true)
                    layer.open({
                        title: '提示'
                        ,content: '商品订阅成功，当商品降价时会发送邮件到您的邮箱。'
                    });
                    else {
                        layer.open({
                            title: '提示'
                            ,content: '您已订阅过该商品，请勿重复订阅。'
                        });
                        $('#subscribe').removeClass('layui-btn-warm').addClass('layui-btn-disabled')
                    }
                },
                //失败/超时
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        log.success('請求超時');
                        setTimeout(function() {
                            log.success('重新请求');
                        }, 2000);
                    }
                }
            });
        }
        function predicateCommodity(commodityId){
            $.ajax({
                url: '/commodity/predict', //地址
                data:{
                    "id": commodityId,
                },
                dataType: 'json', //数据类型
                type: 'GET', //类型
                timeout: 2000, //超时
                //请求成功
                success: function(data, status) {
                    layer.open({
                        title: '预测'
                        ,content: '该商品的不负责任预测价格为：'+data+'元'
                    });
                },
                //失败/超时
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        log.success('請求超時');
                        setTimeout(function() {
                            log.success('重新请求');
                        }, 2000);
                    }
                }
            });
        }
        function getBoard(){
            var html='';
            $.ajax({
                url: '/commodity/top5Hot', //地址
                data:{},
                dataType: 'json', //数据类型
                type: 'GET', //类型
                async:false,
                timeout: 2000, //超时
                //请求成功
                success: function(data, status) {
                console.log(data);

                for(i=0;i<data.total;i++){
                   html+='<li><a>'+data.list[i].cTitle+'</a></li>'
                }
                html='<div class="top-10"><ul>'+html+'</ul></div>'
                    console.log(html.toString())
                },
                //失败/超时
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus === 'timeout') {
                        log.success('請求超時');
                        setTimeout(function() {
                            log.success('重新请求');
                        }, 2000);
                    }
                }
            });
            return html;
        }
        getPrice('www.baidu.com',5)
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        $('#search').click(function () {
            console.log("search")
            getPrice($('#searchVal').val(),5);
        })
        $('#subscribe').click(function () {
            console.log("subscribe")
            subscribeCommodity($("#hiddenCID").val());
        })
        $('#predict').click(function () {
            console.log("predict")
            console.log($("#hiddenCID").val())
            predicateCommodity($("#hiddenCID").val());
        })
        $('#board').click(function () {
           var html=getBoard();
           console.log(html)
            layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '500x'], //宽高
                title:'Top-5',
                content: html.toString()
            });
        })
        var layer
        layui.use('layer', function(){
            layer = layui.layer;
        });
        </script>
        <style>
            a {
                color: #525C66;
                text-decoration: none;
            }
            <!-- div 大小设定 -->
            .top-10 {
                float: left;
                width: 340px;

                margin-top: 106px;
                margin-left: 118px;
                background: #fff;
                border: 1px solid #FFF;
                box-shadow: #d0d0d0 1px 1px 10px 0px;
            }

            .top-10 ul {
                counter-reset: section;
            }

            .top-10 li {
                float: left;
                width: 260px;
                border-bottom: 1px solid #b8c2cc;
                line-height: 46px;
                height: 46px;
                overflow: hidden;
                color: #525C66;
                font-size: 14px;

            }

            .top-10 li:before {
                counter-increment: section;
                content: counter(section);
                display: inline-block;
                padding: 0 12px;
                margin-right: 10px;
                height: 18px;
                line-height: 18px;
                background: #b8c2cc;
                color: #fff;
                border-radius: 3px;
                font-size: 9px
            }
            <!-- 排名前三名颜色控制 -->
            .top-10 li:nth-child(1):before {
                background: #0164b4
            }

            .top-10 li:nth-child(2):before {
                background: #0164b4
            }

            .top-10 li:nth-child(3):before {
                background: #0164b4
            }
        </style>
    </body>
</html>